import { combineReducers, createStore, applyMiddleware } from 'redux';
import type { Store } from 'redux';
import thunk from 'redux-thunk';

import type { ICustomer } from '@/services/customer.d';

import rootReducer from './reducers';

export interface RootState {
  customer: {
    isFetching: boolean;
    requestRes: null | string;
    customerList: ICustomer[];
    customerDetail: ICustomer;
  };
}

/**
 * 记录所有被发起的 action 以及产生的新的 state。
 */
const logger = (store) => (next) => (action) => {
  console.group(action.type);
  console.info('dispatching', action);
  const result = next(action);
  console.log('next state', store.getState());
  console.groupEnd(action.type);
  return result;
};

function configureStore(): Store<RootState> {
  const store = createStore(rootReducer, undefined, applyMiddleware(logger, thunk));
  return store;
}

const storeData = configureStore();

export default storeData;
